package net.bplaced.streetfighter;

import java.util.ArrayList;
import java.util.List;

import net.bplaced.streetfighter.adapter.TeamAdapter;
import net.bplaced.streetfighter.lib.async.task.TeamLoaderTask;
import net.bplaced.streetfighter.lib.model.Team;
import net.bplaced.streetfighter.lib.utils.CallBack;
import net.bplaced.streetfighter.lib.utils.ServiceReturnPackage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class TableActivity
        extends Activity {

    private TeamAdapter teamAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.table);

        final ListView table = (ListView) this.findViewById(R.id.table_list);

        final List<Team> teams = new ArrayList<Team>();
        teams.add(new Team());

        this.teamAdapter = new TeamAdapter(this, teams);

        table.setAdapter(this.teamAdapter);

        final TeamLoaderTask task =
                new TeamLoaderTask(this, this.getResources().getString(R.string.table_progress_message),
                        this.onFinishedLoading);
        task.execute("");
    }

    /**
     * Deklariert einen Callback, welcher zum Ende des Serviceaurufes aufgerufen wird.
     */
    private final CallBack<ServiceReturnPackage<Team[]>> onFinishedLoading =
            new CallBack<ServiceReturnPackage<Team[]>>() {

                @Override
                public void callbackAction(ServiceReturnPackage<Team[]> result) {

                    if (result != null) {
                        if (result.getContent() != null) {

                            TableActivity.this.teamAdapter.addAll(result.getContent());
                        } else {

                            Toast.makeText(TableActivity.this, result.getErrorMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                };
            };
}